/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minimalshift;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.jgrapht.Graph;

/**
 *
 * @author Michal
 */
public class OutputProcessor {

	private Graph<Integer, GraphEdge> graph;
	private Map<GraphEdge, Double> map;
	private List<Integer> reqs;
	private int breakCycle;

	public OutputProcessor(Graph<Integer, GraphEdge> g, Map<GraphEdge, Double> m, List<Integer> r,int t) {
		graph = g;
		map = m;
		reqs = r;
		breakCycle = t;
	}

	public void process() {
		int shifts = 0;
		int cost = 0;
		int missing = 0;
		int over = 0;
		String shiftList = "";
		for (Map.Entry<GraphEdge, Double> e : map.entrySet()) {
			if (e.getValue() == 0) {
				continue;
			}
			GraphEdge edge = e.getKey();
			cost += edge.getCost() * e.getValue();
			if (edge.isShift()) {
				int to = graph.getEdgeTarget(edge);
				if(to>reqs.size()){
					to=breakCycle;
				}
				shiftList = shiftList + "Od: " + graph.getEdgeSource(edge)
						+ " do: " + to + " Pocet lidi:"
						+ e.getValue() + "\n";
				shifts++;
			} else {
				if (edge.isBack()) {
					over += e.getValue();
				} else {
					missing += edge.getCost() * e.getValue();
				}
			}
		}

		System.out.println("Pocet smen: " + shifts + "\ncena: " + cost + "\nchybi lidi: " + missing
				+ "\nprebyva lidi: " + over);
		System.out.println(shiftList);
		
		for(int i = 1;i<=reqs.size();i++){
			int need = reqs.get(i-1);
			List<GraphEdge> edges = getShiftsForTime(i);
			int have = 0;
			for(GraphEdge e : edges){
				have += map.get(e);
			}
			System.out.println("Potrebujeme v case "+i+" lidi: "+ need +" a mame lidi: "+have);
		}
	}
	
	private List<GraphEdge> getShiftsForTime(int time) {
		List<GraphEdge> list = new ArrayList<>();
		for (Map.Entry<GraphEdge, Double> e : map.entrySet()) {
			if (!(e.getValue() > 0)) {
				continue;
			}
			GraphEdge edge = e.getKey();
			if(!edge.isShift())continue;
			int start = graph.getEdgeSource(edge);
			int finish = graph.getEdgeTarget(edge);
			if(finish==(reqs.size()+1)){
				finish = breakCycle;
			}
			
			if(time<finish&&time>=start){
				list.add(edge);
				continue;
			}
			
			if(finish-start<0){
				if (finish > time || start <= time) {
					list.add(edge);
				}
			}
			
		}
		return list;

	}
}
